package com.wx.service.impl;

import com.wx.dto.UserDTO;
import com.wx.dto.UserPageQueryDTO;
import com.wx.dto.UserPwd;
import com.wx.entity.Notice;
import com.wx.entity.User;
import com.wx.exception.BaseException;
import com.wx.mapper.NoticeMapper;
import com.wx.mapper.UserMapper;
import com.wx.result.PageResult;
import com.wx.service.UserService;
import com.wx.vo.NoticeVO;
import com.wx.vo.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public void save(UserDTO userDTO){
        //验证用户名不重复
        User user1 = userMapper.getByUsername(userDTO.getUsername());
        if(user1 != null){
            throw new BaseException("用户名已存在");
        }
        //创建实体类
        User user = new User();

        //复制实体类相同属性
        BeanUtils.copyProperties(userDTO, user);

        //补全属性
        user.setAccountName("好学小孩一枚");
        user.setAvatar("1.png");
        user.setCreateTime(LocalDateTime.now());
        user.setIsFrozen(0);
        user.setBNumber(0);
        user.setReturnNum(0);
        userMapper.save(user);

        //todo:Md5加密
    }

    @Override
    public UserLoginVO login(UserDTO userDTO) {
        User user = userMapper.getByUsername(userDTO.getUsername());
        if(user == null){
            throw new BaseException("用户名不存在");
        }
        if(!user.getPassword().equals(userDTO.getPassword())){
            throw new BaseException("密码错误");
        }
        if(user.getIsFrozen() == 1){
            throw new BaseException("账号被冻结，请联系管理员");
        }

        UserLoginVO userVo = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .build();
        return userVo;
    }

    @Override
    public User getinfo(Integer id) {
        User user = userMapper.getById(id);
        if(user == null){
            throw new BaseException("用户信息加载失败");
        }

        return user;
    }

    @Override
    public void updatepwd(UserPwd userPwd) {
        User user = userMapper.getByUsername(userPwd.getUsername());
        if(!user.getPassword().equals(userPwd.getPassword())){
            throw new BaseException("原密码错误");
        }

        User u = User.builder()
                .id(userPwd.getId())
                .password(userPwd.getNewPassword())
                .build();

        userMapper.update(u);
    }

    @Override
    public void update(UserDTO userDTO) {
        User user = new User();
        if(userDTO.getSex() != null && userDTO.getSex().equals("1")){
            userDTO.setSex("男");
        }else if(userDTO.getSex() != null && userDTO.getSex().equals("0")){
            userDTO.setSex("女");
        }
        BeanUtils.copyProperties(userDTO, user);
        userMapper.update(user);
    }

    //用户分页查询
    @Override
    public PageResult userlist(UserPageQueryDTO userPageQueryDTO) {
        //分页查询插件失效

        //起始索引=（页码-1）*每页记录数      limit 起始索引，页面记录数
        PageResult pageResult = new PageResult();
        userPageQueryDTO.setPage((userPageQueryDTO.getPage()-1)* userPageQueryDTO.getPageSize());

        pageResult.setTotal(userMapper.getUserCount(userPageQueryDTO));
        pageResult.setRecords(userMapper.getUserList(userPageQueryDTO));
        return pageResult;

    }

    /*根据用户名获取notice*/
    @Override
    public List<NoticeVO> getnotice(Notice notice) {

        List<NoticeVO> list = noticeMapper.getNoticeByUsername(notice);

        return list;
    }

    /*用户收到通知*/
    @Override
    public void receive(Notice notice) {
        noticeMapper.receiveNotice(notice);
    }

}
